﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace MusicTag
{
    public partial class Form1 : Form
    {
        cellVal cv = new cellVal();

        public Form1()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
  
        }

        void loadFiles(string input)
        {
            string[] filePaths = Directory.GetFiles(input,"*.mp3");
            foreach (string s in filePaths)
            {
                dataGridView1.Rows.Add(s);
            }

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //Clear all the boxes
            titleBOX.Clear();
            albumBOX.Clear();
            artistBOX.Clear();
            yearBOX.Clear();
            genreBOX.Clear();
            trackBOX.Clear();
            pictureBox1.Image = null;

            //Get value of cell that was clicked
            string celVal = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString();
            dataGridView1.Rows[e.RowIndex].Selected = true;
            
            //Send celVal (value of the cell user clicked)
            //to cellVal class
            cv.SetName(celVal);
            
            //initilize TagLib. Get path from cellVal class 'GetName'
            TagLib.File file = TagLib.File.Create(cv.GetName());

            titleBOX.Text = file.Tag.Title;
            albumBOX.Text = file.Tag.Album;
            trackBOX.Text = file.Tag.Track.ToString();

            //Get album art
            if (file.Tag.Pictures.Length >= 1)
            {
                var bin = (byte[])(file.Tag.Pictures[0].Data.Data);
                pictureBox1.Image = Image.FromStream(new MemoryStream(bin)).GetThumbnailImage(140, 140, null, IntPtr.Zero);
            }

            //Get year as long as it is not empty
            if (file.Tag.Year != 0)
            {
                yearBOX.Text = file.Tag.Year.ToString();
            }
            
            //Get Genre
            foreach (string gen in file.Tag.Genres)
            {
                genreBOX.AppendText(gen + System.Environment.NewLine);   
            }
            
            //Get Album Artist(s) (could be more then one)
            foreach (string  arts in file.Tag.AlbumArtists)
            {
                artistBOX.AppendText(arts + System.Environment.NewLine);   
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            //initilize TagLib. Get path from cellVal class 'GetName'
            TagLib.File file = TagLib.File.Create(cv.GetName());

            string[] genresARR = genreBOX.Text.Split(new char[] { '\n' });
            string[] albumArtistARR = artistBOX.Text.Split(new char[] { '\n' });
            
            uint yearConv = Convert.ToUInt32(yearBOX.Text);
            uint trackConv = Convert.ToUInt32(trackBOX.Text);
            
            file.Tag.Title = titleBOX.Text;
            file.Tag.Genres = genresARR;
            file.Tag.AlbumArtists = albumArtistARR;
            file.Tag.Year = yearConv;
            file.Tag.Album = albumBOX.Text;
            file.Tag.Track = trackConv;
            file.Save();
        }

        private void openFileLocationToolStripMenuItem_Click(object sender, EventArgs e)
        { 
            songManager.OpenSongLocation(cv.GetName());
        }

        private void playToolStripMenuItem_Click(object sender, EventArgs e)
        {
            songManager.PlaySong(cv.GetName());
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            songManager.OpenSongLocation(cv.GetName());
        }

        private void setMusicFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Opens folder browser and gets the folder user selects
            folderBrowserDialog1.Description = "Select the folder containing the music";
            folderBrowserDialog1.ShowDialog();

            //Sets the path on songManager
            songManager.FilePath(folderBrowserDialog1.SelectedPath);

            //Calls the method to load the data grid with the files
            loadFiles(folderBrowserDialog1.SelectedPath);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form aboutThis = new About();
            aboutThis.Show();
        }

        private void musicTaggerHomepageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Open codeplex page
            Process.Start("http://musictagger.codeplex.com/");
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //initilize TagLib. Get path from cellVal class 'GetName'
            TagLib.File file = TagLib.File.Create(cv.GetName());

            string[] genresARR = genreBOX.Text.Split(new char[] { '\n' });
            string[] albumArtistARR = artistBOX.Text.Split(new char[] { '\n' });

            uint yearConv = Convert.ToUInt32(yearBOX.Text);
            uint trackConv = Convert.ToUInt32(trackBOX.Text);

            file.Tag.Title = titleBOX.Text;
            file.Tag.Genres = genresARR;
            file.Tag.AlbumArtists = albumArtistARR;
            file.Tag.Year = yearConv;
            file.Tag.Album = albumBOX.Text;
            file.Tag.Track = trackConv;
            file.Save(); 
        }
    }
}
